package com.sangfor.ssl.service.https;

import android.os.Looper;
import android.os.NetworkOnMainThreadException;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import java.io.Closeable;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* loaded from: classes2.dex */
public final class HttpRequest implements Closeable {
    public static final int DEFAULT_CONNECT_TIME = 10;
    public static final int DEFAULT_READ_WRITE_TIME = 15;
    private static final int NATIVE_CERT_VERIFY = 5;
    private static final int NATIVE_DATA_SOURCE = 2;
    private static final int NATIVE_DATA_TARGET = 3;
    private static final int NATIVE_HTTP_REQUEST = 1;
    private static final int NATIVE_WEAK_REF = 4;
    private static final String TAG = "HttpRequest";
    private static final boolean TRACE_CLOSE = true;
    private DataSource mDataSource;
    private DataTarget mDataTarget;
    private DownloadProgressListener mDownloadProgressListener;
    private HttpInputStream mInputStream;
    private long mNativeCertVerifyPtr;
    private long mNativeDataSourcePtr;
    private long mNativeDataTargetPtr;
    private final long mNativePtr;
    private CertificateVerifier mVerifier;
    private boolean mClosed = false;
    private Throwable mStackTrace = new Throwable();
    private final long mNativeWeakSelfPtr = createWeakGlobalRefNative(this);

    /* loaded from: classes2.dex */
    public interface DataSource {
        int getSize();

        int onRead(ByteBuffer byteBuffer) throws SFException;
    }

    /* loaded from: classes2.dex */
    public interface DataTarget {
        int onWrite(ByteBuffer byteBuffer) throws SFException;

        void reset();

        void setSize(int i);
    }

    /* loaded from: classes2.dex */
    public interface DownloadProgressListener {
        int onProgressUpdated(long j, long j2);
    }

    private static native long createWeakGlobalRefNative(Object obj);

    private static native void destroyObjectNative(long j, int i);

    private static native String getCookiesNative(long j);

    private static native String[][] getHeaderListNative(long j) throws SFException;

    private static native String getHeadersNative(long j) throws SFException;

    private static native long getInputStreamNative(long j) throws SFException;

    private static native long getNative(String str, String[] strArr, String[] strArr2);

    private static native long getResponseCodeNative(long j) throws SFException;

    private static native String getResponseNative(long j) throws SFException;

    private static native long postNative(String str, String[] strArr, String[] strArr2);

    private static native void setCertNative(long j, String str, String str2, int i);

    private static native long setCertificateVerifierNative(long j, long j2, boolean z);

    private static native void setCipherNative(long j, String str);

    private static native void setConnectTimeoutNative(long j, int i);

    private static native void setCookiesNative(long j, String str);

    private static native long setDataSourceNative(long j, long j2, boolean z);

    private static native long setDataTargetNative(long j, long j2, boolean z);

    private static native void setDownloadProgressListenerNative(long j, long j2, boolean z);

    private static native void setFollowRedirectsNative(long j, boolean z);

    private static native void setGMVersionNative(long j, long j2);

    private static native void setKeepAliveNative(long j, boolean z);

    private static native void setPostBodyNative(long j, String str);

    private static native void setPostDataNative(long j, byte[] bArr);

    private static native void setPostFileNative(long j, String[] strArr, String[] strArr2);

    private static native void setReadTimeoutNative(long j, int i);

    private static native void setRequestHeaderNative(long j, String str, String str2);

    public static HttpRequest get(String str) {
        return get(str, null);
    }

    public static HttpRequest get(String str, Map<String, String> map) {
        long j;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("url cannot be empty");
        }
        if (map == null || map.isEmpty()) {
            j = getNative(str, null, null);
        } else {
            int size = map.size();
            String[] strArr = new String[size];
            String[] strArr2 = new String[size];
            int i = 0;
            for (Map.Entry<String, String> entry : map.entrySet()) {
                strArr[i] = entry.getKey();
                strArr2[i] = entry.getValue();
                if (strArr2[i] == null) {
                    strArr2[i] = "";
                }
                i++;
            }
            j = getNative(str, strArr, strArr2);
        }
        return new HttpRequest(j);
    }

    public static HttpRequest post(String str) {
        return post(str, null);
    }

    public static HttpRequest post(String str, Map<String, String> map) {
        long postNative;
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("url cannot be empty");
        }
        if (map == null || map.isEmpty()) {
            postNative = postNative(str, null, null);
        } else {
            int size = map.size();
            String[] strArr = new String[size];
            String[] strArr2 = new String[size];
            int i = 0;
            for (Map.Entry<String, String> entry : map.entrySet()) {
                strArr[i] = entry.getKey();
                strArr2[i] = entry.getValue();
                if (strArr2[i] == null) {
                    strArr2[i] = "";
                }
                i++;
            }
            postNative = postNative(str, strArr, strArr2);
        }
        return new HttpRequest(postNative);
    }

    public HttpRequest setPostBody(String str) {
        ensureNotClosed();
        if (str == null) {
            str = "";
        }
        setPostBodyNative(this.mNativePtr, str);
        return this;
    }

    public HttpRequest setPostFile(FileInfo fileInfo) {
        checkNotNull(fileInfo, "fileInfo cannot be null");
        ensureNotClosed();
        String[][] arrays = fileInfo.toArrays();
        setPostFileNative(this.mNativePtr, arrays[0], arrays[1]);
        return this;
    }

    public HttpRequest setPostData(byte[] bArr) {
        checkNotNull(bArr, "data cannot be null");
        ensureNotClosed();
        setPostDataNative(this.mNativePtr, bArr);
        return this;
    }

    private void dispatchDataTargetSetSize(int i) {
        DataTarget dataTarget = this.mDataTarget;
        if (dataTarget != null) {
            dataTarget.setSize(i);
        } else {
            Log.error(TAG, "DataTarget not exist");
        }
    }

    private void dispatchDataTargetReset() {
        DataTarget dataTarget = this.mDataTarget;
        if (dataTarget != null) {
            dataTarget.reset();
        } else {
            Log.error(TAG, "DataTarget not exist");
        }
    }

    private int dispatchDataTargetOnWrite(ByteBuffer byteBuffer) throws SFException {
        DataTarget dataTarget = this.mDataTarget;
        if (dataTarget != null) {
            return dataTarget.onWrite(byteBuffer);
        }
        Log.error(TAG, "DataTarget not exist");
        return -1;
    }

    public HttpRequest setDataTarget(DataTarget dataTarget) {
        ensureNotClosed();
        long dataTargetNative = setDataTargetNative(this.mNativePtr, this.mNativeWeakSelfPtr, dataTarget == null);
        if (dataTarget != null && dataTargetNative == 0) {
            Log.error(TAG, "setDataTargetNative failed");
            return this;
        }
        this.mDataTarget = dataTarget;
        long j = this.mNativeDataTargetPtr;
        if (j != 0) {
            destroyObjectNative(j, 3);
        }
        this.mNativeDataTargetPtr = dataTargetNative;
        return this;
    }

    public String getHeaders() throws SFException {
        ensureNotClosed();
        return getHeadersNative(this.mNativePtr);
    }

    public Map<String, List<String>> getHeaderList() throws SFException {
        ensureNotClosed();
        String[][] headerListNative = getHeaderListNative(this.mNativePtr);
        if (headerListNative == null) {
            return null;
        }
        HashMap hashMap = new HashMap(headerListNative.length);
        for (String[] strArr : headerListNative) {
            hashMap.put(strArr[0], Arrays.asList(strArr).subList(1, strArr.length));
        }
        return hashMap;
    }

    public String getResponse() throws SFException {
        ensureNotClosed();
        return getResponseNative(this.mNativePtr);
    }

    public long getResponseCode() throws SFException {
        ensureNotClosed();
        return getResponseCodeNative(this.mNativePtr);
    }

    public InputStream getInputStream() throws SFException {
        ensureNotClosed();
        HttpInputStream httpInputStream = this.mInputStream;
        if (httpInputStream != null) {
            return httpInputStream;
        }
        long inputStreamNative = getInputStreamNative(this.mNativePtr);
        if (inputStreamNative == 0) {
            throw new SFException("getInputStreamNative failed");
        }
        HttpInputStream httpInputStream2 = new HttpInputStream(inputStreamNative);
        this.mInputStream = httpInputStream2;
        return httpInputStream2;
    }

    private int dispatchDataSourceGetSize() {
        DataSource dataSource = this.mDataSource;
        if (dataSource != null) {
            return dataSource.getSize();
        }
        Log.error(TAG, "DataSource not exist");
        return -1;
    }

    private int dispatchDataSourceOnRead(ByteBuffer byteBuffer) throws SFException {
        DataSource dataSource = this.mDataSource;
        if (dataSource != null) {
            return dataSource.onRead(byteBuffer);
        }
        Log.error(TAG, "DataSource not exist");
        return -1;
    }

    public HttpRequest setDataSource(DataSource dataSource) {
        ensureNotClosed();
        long dataSourceNative = setDataSourceNative(this.mNativePtr, this.mNativeWeakSelfPtr, dataSource == null);
        if (dataSource != null && dataSourceNative == 0) {
            Log.error(TAG, "setDataSourceNative failed");
            return this;
        }
        this.mDataSource = dataSource;
        long j = this.mNativeDataSourcePtr;
        if (j != 0) {
            destroyObjectNative(j, 2);
        }
        this.mNativeDataSourcePtr = dataSourceNative;
        return this;
    }

    public HttpRequest setCert(String str, String str2, CertType certType) {
        checkNotNull(str, "cert cannot be null");
        ensureNotClosed();
        if (str2 == null) {
            str2 = "";
        }
        if (certType == null) {
            certType = CertType.CERT_TYPE_BUS;
        }
        setCertNative(this.mNativePtr, str, str2, certType.intValue());
        return this;
    }

    public String getCookies() {
        ensureNotClosed();
        return getCookiesNative(this.mNativePtr);
    }

    public HttpRequest setCookies(String str) {
        ensureNotClosed();
        if (str == null) {
            str = "";
        }
        setCookiesNative(this.mNativePtr, str);
        return this;
    }

    public HttpRequest setCookies(Map<String, String> map) {
        ensureNotClosed();
        StringBuilder sb = new StringBuilder("");
        if (map != null && !map.isEmpty()) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                sb.append(entry.getKey());
                sb.append('=');
                sb.append(entry.getValue());
                sb.append(';');
            }
            sb.deleteCharAt(sb.length() - 1);
        }
        setCookiesNative(this.mNativePtr, sb.toString());
        return this;
    }

    public HttpRequest setKeepAlive(boolean z) {
        ensureNotClosed();
        setKeepAliveNative(this.mNativePtr, z);
        return this;
    }

    public HttpRequest setCipher(String str) {
        ensureNotClosed();
        if (str == null) {
            str = "";
        }
        setCipherNative(this.mNativePtr, str);
        return this;
    }

    public HttpRequest setGMVersion(long j) {
        ensureNotClosed();
        setGMVersionNative(this.mNativePtr, j);
        return this;
    }

    public HttpRequest setFollowRedirects(boolean z) {
        ensureNotClosed();
        setFollowRedirectsNative(this.mNativePtr, z);
        return this;
    }

    public HttpRequest setConnectTimeout(int i) {
        ensureNotClosed();
        setConnectTimeoutNative(this.mNativePtr, i);
        return this;
    }

    public HttpRequest setReadTimeout(int i) {
        ensureNotClosed();
        setReadTimeoutNative(this.mNativePtr, i);
        return this;
    }

    public HttpRequest setCertificateVerifier(CertificateVerifier certificateVerifier) {
        ensureNotClosed();
        long certificateVerifierNative = setCertificateVerifierNative(this.mNativePtr, this.mNativeWeakSelfPtr, certificateVerifier == null);
        if (certificateVerifier != null && certificateVerifierNative == 0) {
            Log.error(TAG, "setCertificateVerifierNative failed");
            return this;
        }
        this.mVerifier = certificateVerifier;
        long j = this.mNativeCertVerifyPtr;
        if (j != 0) {
            destroyObjectNative(j, 5);
        }
        this.mNativeCertVerifyPtr = certificateVerifierNative;
        return this;
    }

    private boolean dispatchCertificateVerifier(SSLSession sSLSession) {
        if (sSLSession == null) {
            return this.mVerifier.verify(new SSLSession());
        }
        return this.mVerifier.verify(sSLSession);
    }

    public HttpRequest setRequestHeader(String str, String str2) {
        checkNotNull(str, "name cannot be null");
        ensureNotClosed();
        if (str2 == null) {
            str2 = "";
        }
        setRequestHeaderNative(this.mNativePtr, str, str2);
        return this;
    }

    public HttpRequest setDownloadProgressListener(DownloadProgressListener downloadProgressListener) {
        ensureNotClosed();
        this.mDownloadProgressListener = downloadProgressListener;
        setDownloadProgressListenerNative(this.mNativePtr, this.mNativeWeakSelfPtr, downloadProgressListener == null);
        return this;
    }

    private int dispatchProgressUpdated(long j, long j2) {
        DownloadProgressListener downloadProgressListener = this.mDownloadProgressListener;
        if (downloadProgressListener != null) {
            return downloadProgressListener.onProgressUpdated(j, j2);
        }
        Log.error(TAG, "mDownloadProgressListener not exist");
        return -1;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws SFException {
        if (this.mClosed) {
            throw new IllegalStateException("invoke method on a closed object");
        }
        this.mClosed = true;
        this.mStackTrace = null;
        destroyObjectNative(this.mNativeWeakSelfPtr, 4);
        destroyObjectNative(this.mNativeDataSourcePtr, 2);
        destroyObjectNative(this.mNativeDataTargetPtr, 3);
        destroyObjectNative(this.mNativePtr, 1);
        destroyObjectNative(this.mNativeCertVerifyPtr, 5);
    }

    protected void finalize() throws Throwable {
        if (!this.mClosed) {
            Log.warn(TAG, "HttpRequest instance not closed, create by ", this.mStackTrace);
            close();
        }
        super.finalize();
    }

    private void ensureNotClosed() {
        if (this.mClosed) {
            throw new IllegalStateException("invoke method on a closed object");
        }
    }

    private void ensureNotOnMainThread() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            throw new NetworkOnMainThreadException();
        }
    }

    private static <T> T checkNotNull(T t, String str) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException(str);
    }

    private HttpRequest(long j) {
        this.mNativePtr = j;
    }
}
